package com.central.apps.handler;

import com.central.app.common.base.BaseHandler;
import com.central.app.service.HandlerService;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;
@Component
public class AllocationassetAppHandler extends LvBaseHandler {

    @Autowired private HandlerService handlerService;

    @Override
    public void afterExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op, String event) throws Exception {
        if(flowNode.getId().equals("spr")){
            //1.获取资产调拨子表的 目的位置 字段,更新到台账位置信息
            //2.在资产台账资产调拨标签页 插入当前调拨信息

            //1.获取资产调拨子表的 资产编号,目的位置字段
            String getLocaSql ="select assetnum,targetlocationnum,targetlocation,usedepartment,Responsible from bl_transferline where transfernum =(select transfernum from bl_allocationasset where id ="+id+")";
            List<Map<String, Object>> getLocabySql = handlerService.findBySql(getLocaSql);
            String targetlocationnum = (String) getLocabySql.get(0).get("targetlocationnum");
            String targetlocation = (String) getLocabySql.get(0).get("targetlocation");
            String assetnum = (String) getLocabySql.get(0).get("assetnum");
            //更新到资产台账
            String updLocaSql ="update bl_asset set assetlocation ='"+targetlocationnum+"', locationdesc ='"+targetlocation+"' where assetnum ='"+assetnum+"';";
            handlerService.updateBySql(updLocaSql);

            //2.在资产台账资产调拨标签页 插入当前调拨信息
            //获取调拨子表信息
            String getDBSoninfosSql ="select * from bl_allocationasset where id ="+id;
            List<Map<String, Object>> getDBSoninfosBySql = handlerService.findBySql(getDBSoninfosSql);
            String transfernum = (String) getDBSoninfosBySql.get(0).get("transfernum");
            String user1 = (String) getDBSoninfosBySql.get(0).get("user1");
            String removeunit = (String) getDBSoninfosBySql.get(0).get("removeunit");
            String removeleader = (String) getDBSoninfosBySql.get(0).get("removeleader");
            String moveleader = (String) getDBSoninfosBySql.get(0).get("moveleader");
            String dept1 = (String) getDBSoninfosBySql.get(0).get("dept1");
            String orgcode = (String) getDBSoninfosBySql.get(0).get("orgcode");
            String dept2 = (String) getDBSoninfosBySql.get(0).get("dept2");
            String status = (String) getDBSoninfosBySql.get(0).get("status");
            String deptcode = (String) getDBSoninfosBySql.get(0).get("deptcode");
            String dept3 = (String) getDBSoninfosBySql.get(0).get("dept3");
            String dept4 = (String) getDBSoninfosBySql.get(0).get("dept4");
            String sitecode = (String) getDBSoninfosBySql.get(0).get("sitecode");
            String remark = (String) getDBSoninfosBySql.get(0).get("remark");
            String createuserdesc = (String) getDBSoninfosBySql.get(0).get("createuserdesc");
            String moveunit = (String) getDBSoninfosBySql.get(0).get("moveunit");
            String user2 = (String) getDBSoninfosBySql.get(0).get("user2");
            String movecost = (String) getDBSoninfosBySql.get(0).get("movecost");
            String removecost = (String) getDBSoninfosBySql.get(0).get("removecost");
            String description = (String) getDBSoninfosBySql.get(0).get("description");
            Date createtime = (Date) getDBSoninfosBySql.get(0).get("createtime");
            //插入到台账下调拨表
            String insertSql ="insert into bl_glzcdb (transfernum,user1,removeunit,removeleader,moveleader,dept1,orgcode,dept2,status,deptcode,dept3,dept4,sitecode,remark,createuserdesc,moveunit,user2,movecost,removecost,description,createtime,assetnum)"+
                    " values('"+transfernum+"','"+user1+"','"+removeunit+"','"+removeleader+"','"+moveleader+"','"+dept1+"','"+orgcode+"','"+dept2+"','"+status+"','"+deptcode+"','"+dept3+"','"+dept4+"','"+sitecode+"','"+remark+"','"+createuserdesc+"','"+moveunit+"','"+user2+"','"+movecost+"','"+removecost+"','"+description+"','"+createtime+"','"+assetnum+"');";
            handlerService.updateBySql(insertSql);
            //将移入单位字段dept2变更到台账下使用部门usedepartment,责任部门Responsible
            String updSql ="update bl_asset set usedepartment ='"+dept2+"',Responsible ='"+dept2+"' where assetnum ='"+assetnum+"';";
            handlerService.updateBySql(updSql);
        }
    }
}
